// Copyright (c) 2004-2008 Dmitry Shuklin. All rights reserved.

//#define DRAG_AND_DROP_SUPPORT

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Cerebrum.Direct3D.Forms
{
#if DRAG_AND_DROP_SUPPORT
	#region DRAG AND DROP
	public class DragControlsEventArgs: EventArgs
	{		
		public DragControlsEventArgs(System.Windows.Forms.Control DragTarget, System.Windows.Forms.Control[] DragSource) 
		{
			this.m_DragTarget=DragTarget;
			this.m_DragSource=DragSource;
		}

		protected DragDropEffects m_Effect = DragDropEffects.None;
		public DragDropEffects Effect
		{
			get
			{
				return m_Effect;
			}
			set
			{
				m_Effect = value;
			}
		}

		public DragControlsEventArgs() 
		{
			this.m_DragTarget=null;
			this.m_DragSource=null;
		}

		protected System.Windows.Forms.Control m_DragTarget;
		public System.Windows.Forms.Control DragTarget
		{
			get 
			{
				return m_DragTarget;
			}
			set
			{	
				m_DragTarget = value;									
			}
		}
	

		protected System.Windows.Forms.Control[] m_DragSource;			
		public System.Windows.Forms.Control[] DragSource
		{
			get 
			{
				return m_DragSource;
			}
			set
			{	
				m_DragSource = value;									
			}
		}

	}
	
	public delegate void DragControlsEventHandler(object sender, DragControlsEventArgs me);	
	#endregion
#endif

	public interface ITabElement
	{
		bool Fellowship {get;set;}
		bool Visible {get;set;}
	}

	public interface ITabButton : ITabElement
	{
		bool Checked {get;set;}
		event System.EventHandler CheckedChanged;
	}

	public class TabPage : Cerebrum.Direct3D.Forms.Control, ITabElement
	{
		protected ITabButton m_HeaderButton;
		[System.ComponentModel.DefaultValue(null)]
		public ITabButton HeaderButton
		{
			get
			{
				return m_HeaderButton;
			}
			set
			{
				if(m_HeaderButton!=value)
				{
					if(m_HeaderButton!=null)
					{
						m_HeaderButton.CheckedChanged -= new System.EventHandler(HeaderButton_CheckedChanged);
					}
					m_HeaderButton = value;
					if(m_HeaderButton!=null)
					{
						m_HeaderButton.CheckedChanged += new System.EventHandler(HeaderButton_CheckedChanged);
					}
				}
			}
		}

		protected System.Windows.Forms.MenuItem m_MenuItem;
		[System.ComponentModel.DefaultValue(null)]
		public System.Windows.Forms.MenuItem MenuItem
		{
			get
			{
				return m_MenuItem;
			}
			set
			{
				m_MenuItem = value;
			}
		}

		private void HeaderButton_CheckedChanged(object sender, System.EventArgs e)
		{
			OnHeaderButtonCheckedChanged(sender, e);
		}
		protected virtual void OnHeaderButtonCheckedChanged(object sender, System.EventArgs e)
		{
			this.Visible = m_HeaderButton.Checked;
		}
		
		internal protected override void OnVisibleChanged(System.EventArgs e)
		{
			try
			{ // something wrong in MenuItem
				if ( m_MenuItem != null)
				{
					m_MenuItem.Visible = this.Visible;
				}
			}
			catch
			{
			}
			base.OnVisibleChanged(e);
		}

		internal protected override void OnEnabledChanged(System.EventArgs e)
		{
			try
			{
			if ( m_MenuItem != null)
			{
				m_MenuItem.Enabled = this.Enabled;
			}
			}
			catch
			{
			}
			base.OnEnabledChanged(e);
		}

		#region ITabElement Members

		private bool m_MyVisible = true;
		[System.ComponentModel.DefaultValue(true)]
		public new bool Visible 
		{
			set 
			{
				if( value != m_MyVisible ) 
				{
					m_MyVisible = value;
					TabHeader header = null;
					if( m_TabFellowship && (header = (this.Parent as TabHeader)) != null )
					{
						header.RefreshLayout();
					}
					else
					{
						base.Visible = m_MyVisible;
					}
				}
			}
			get 
			{
				return m_MyVisible;
			}
		}

		protected bool m_TabFellowship = false;
		[System.ComponentModel.DefaultValue(false)]
		public bool Fellowship
		{
			get
			{
				return m_TabFellowship;
			}
			set
			{
				m_TabFellowship = value;
			}
		}

		#endregion
	}


	public class TabButton : ImageButton, ITabButton
	{	
		/*[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),	]
		public new bool AllowDrop 
		{
			get 
			{
				return base.AllowDrop;
			}
			set 
			{				
				base.AllowDrop = value;
			}
		}
		
		public bool BaseVisible 
		{
			get 
			{
				return base.Visible;
			}
			set 
			{
				base.Visible = value;
			}
		}*/



		#region ITabElement Members

		private bool m_MyVisible = true;
		[System.ComponentModel.DefaultValue(true)]
		public new bool Visible 
		{
			set 
			{
				if( value != m_MyVisible ) 
				{
					m_MyVisible = value;
					TabHeader header = null;
					if( m_TabFellowship && (header = (this.Parent as TabHeader)) != null )
					{
						header.RefreshLayout();
					}
					else
					{
						base.Visible = m_MyVisible;
					}
				}
			}
			get 
			{
				return m_MyVisible;
			}
		}

		protected bool m_TabFellowship = true;
		[System.ComponentModel.DefaultValue(true)]
		public bool Fellowship
		{
			get
			{
				return m_TabFellowship;
			}
			set
			{
				m_TabFellowship = value;
			}
		}

		#endregion
	}

	public class TabLabel : Label, ITabButton
	{

		#region ITabElement Members

		private bool m_MyVisible = true;
		[System.ComponentModel.DefaultValue(true)]
		public new bool Visible 
		{
			set 
			{
				if( value != m_MyVisible ) 
				{
					m_MyVisible = value;
					TabHeader header = null;
					if( m_TabFellowship && (header = (this.Parent as TabHeader)) != null )
					{
						header.RefreshLayout();
					}
					else
					{
						base.Visible = m_MyVisible;
					}
				}
			}
			get 
			{
				return m_MyVisible;
			}
		}

		protected bool m_TabFellowship = true;
		[System.ComponentModel.DefaultValue(true)]
		public bool Fellowship
		{
			get
			{
				return m_TabFellowship;
			}
			set
			{
				m_TabFellowship = value;
			}
		}

		#endregion

		#region ITabButton Members

		public event System.EventHandler CheckedChanged
		{
			add	{	this.GetEventHandlerList().AddHandler(CheckedChangedEvent, value);	}
			remove	{	this.GetEventHandlerList().RemoveHandler(CheckedChangedEvent, value);	}
		}


		protected bool m_Checked = false;
		[System.ComponentModel.DefaultValue(false)]
		public bool Checked
		{
			get 
			{
				return m_Checked;
			}
			set
			{
				if(m_Checked != value)
				{
					m_Checked = value;
					//this.Invalidate();
					OnCheckedChanged(System.EventArgs.Empty);
				}
			}
		}

		#endregion
		
		protected virtual void OnCheckedChanged(System.EventArgs e)
		{
			EventHandler handler = (System.EventHandler)this.GetEventHandlerList()[CheckedChangedEvent];
			if (handler != null)
				handler(this, e);
		}
	}

	public enum LayoutDirection
	{
		Horizontal,
		Vertical
	}

	public enum SelectionBehavior
	{
		SingleSelect,
		MultiSelect,
		ManualSelect
	}
	///
	///
	///
	/// <summary>
	/// Summary description for TabHeader.
	/// </summary>
	///
	///
	public class TabHeader : TabPage 
	{

		// private fields
		private ImageButton m_ScrollLeftButton;
		private ImageButton m_ScrollRightButton;		
		private ITabButton m_SelectedButton;
		private ITabButton m_LastShiftButton;
		//private bool m_LastShiftState;
		private int m_LeftFirst;		
		private bool m_Scrolling; 
		private LayoutDirection m_Direction; 
		private SelectionBehavior m_Behavior;

		// proprties
		[System.ComponentModel.DefaultValue(SelectionBehavior.SingleSelect)]
		public SelectionBehavior Behavior 
		{
			get 
			{
				return m_Behavior;
			}
			set 
			{
				m_Behavior = value;
				RefreshLayout();
			}
		}

		[System.ComponentModel.DefaultValue(LayoutDirection.Horizontal)]
		public LayoutDirection Direction 
		{
			get 
			{
				return m_Direction;
			}
			set 
			{
				m_Direction = value;
				RefreshLayout();
			}
		}

		protected static readonly object SelectedTabChangedEvent = new object();

		public event System.EventHandler SelectedTabChanged
		{
			add	{	this.GetEventHandlerList().AddHandler(SelectedTabChangedEvent, value);	}
			remove	{	this.GetEventHandlerList().RemoveHandler(SelectedTabChangedEvent, value);	}
		}
		protected void OnSelectedTabChanged(System.EventArgs e)
		{
			EventHandler handler = (System.EventHandler)this.GetEventHandlerList()[SelectedTabChangedEvent];
			if (handler != null)
				handler(this, e);
		}

		[System.ComponentModel.DefaultValue(null)]
		public ITabButton SelectedButton 
		{
			get 
			{
				return m_SelectedButton;
			}
			set 
			{
				if( m_SelectedButton == value )
					return;
 
				ITabButton oldTab = m_SelectedButton;

				m_SelectedButton = value;

				if( m_Behavior == SelectionBehavior.SingleSelect && oldTab != null )
					oldTab.Checked = false;

				if( (m_SelectedButton != null) && (m_Behavior != SelectionBehavior.ManualSelect) && (!m_SelectedButton.Checked) )
					m_SelectedButton.Checked = true;

				RefreshLayout();
				
				if(oldTab != m_SelectedButton)
					OnSelectedTabChanged(System.EventArgs.Empty);
			}
		}

		[System.ComponentModel.DefaultValue(null)]
		public ImageButton ScrollLeftButton
		{
			get
			{
				return m_ScrollLeftButton;
			}
			set 
			{
				if(m_ScrollLeftButton != value)
				{
					if(m_ScrollLeftButton!=null)
					{
						m_ScrollLeftButton.Click -= new MouseEventHandler(ScrollLeftButton_Click);
					}
					m_ScrollLeftButton = value;
					if(m_ScrollLeftButton!=null)
					{
						m_ScrollLeftButton.Visible = false;
						m_ScrollLeftButton.Click += new MouseEventHandler(ScrollLeftButton_Click);
					}
				}
			}
		}

		[System.ComponentModel.DefaultValue(null)]
		public ImageButton ScrollRightButton
		{
			get
			{
				return m_ScrollRightButton;
			}
			set 
			{
				if(m_ScrollRightButton!=value)
				{					
					if(m_ScrollRightButton!=null)
					{
						m_ScrollRightButton.Click -= new MouseEventHandler(ScrollRightButton_Click);
					}
					m_ScrollRightButton = value;
					if(m_ScrollRightButton!=null)
					{
						m_ScrollRightButton.Visible = false;
						m_ScrollRightButton.Click += new MouseEventHandler(ScrollRightButton_Click);
					}
				}
			}
		}

		
#if DRAG_AND_DROP_SUPPORT
		#region DRAG AND DROP
		public event DragControlsEventHandler DragDropButtons;
		public event DragControlsEventHandler DragOverButtons;
		
		protected virtual void OnDragDropButtons(DragControlsEventArgs de)
		{
			if(DragDropButtons!=null)
			{
				DragDropButtons(this, de);
			}
		}
		
		protected virtual void OnDragOverButtons(DragControlsEventArgs de)
		{
			if(DragOverButtons!=null)
			{
				DragOverButtons(this, de);
			}
		}

		private void Control_DragStarting(object sender, DragEventArgs e)
		{
			ArrayList ArrayTabButton =new ArrayList();// 

			ITabButton currentButton = null;						
			if( typeof(ITabButton).IsAssignableFrom(sender.GetType()) ) 	currentButton = (ITabButton)sender;				
			
			if(currentButton != null) 
			{						 
				if (currentButton.Checked)
				{			
					foreach( Control c in Controls ) 
					{
						if( typeof(ITabButton).IsAssignableFrom(c.GetType()) ) 
						{							
							ITabButton tabButton = (ITabButton)c; 
							if( tabButton.Visible && tabButton.Checked &&tabButton.AllowDrag) 
							{
								ArrayTabButton.Add(tabButton);																		
							}
						}
					} // foreach					
				}
				else
				{
					ArrayTabButton.Add(currentButton);	
				}	
			}						
				
			e.Data.SetData(ArrayTabButton.ToArray(typeof(System.Windows.Forms.Control)));			
		}

		
		private void Control_DragDrop(object sender, DragEventArgs de) 
		{			
			DragControlsEventArgs me =new DragControlsEventArgs();

			me.DragTarget = (System.Windows.Forms.Control) sender;
			me.DragSource=(System.Windows.Forms.Control[])de.Data.GetData(de.Data.GetFormats(false)[0]);										
			
			OnDragDropButtons(me);
			de.Effect = me.Effect;
		}
			
		private void Control_DragOver(object sender, DragEventArgs de)
		{
			DragControlsEventArgs me =new DragControlsEventArgs();

			me.DragTarget = (System.Windows.Forms.Control) sender;
			me.DragSource=(System.Windows.Forms.Control[])de.Data.GetData(de.Data.GetFormats(false)[0]);										
			
			OnDragOverButtons(me);	
			de.Effect = me.Effect;
		}
		#endregion
#endif
		///
		///
		///
		public TabHeader() 
		{
			m_LeftFirst = 0;
			m_Scrolling = false;
			m_Behavior = SelectionBehavior.SingleSelect;;
			m_Direction = LayoutDirection.Horizontal; 
			m_SelectedButton = null;
			m_LastShiftButton = null;
			//m_LastShiftState = true;

#if DRAG_AND_DROP_SUPPORT
			this.DragOver+=new DragEventHandler(Control_DragOver);
			this.DragDrop+=new DragEventHandler(Control_DragDrop);
#endif
		}
           

		protected internal override void OnControlAdded(ControlEventArgs e)
		{
			if( typeof(ITabButton).IsAssignableFrom(e.Control.GetType()) ) 
			{
				ITabButton tabButton = (ITabButton)e.Control;
				e.Control.Click += new MouseEventHandler(TabButton_OnClick); 
				tabButton.CheckedChanged += new EventHandler(TabButton_OnCheckedChanged); 

#if DRAG_AND_DROP_SUPPORT
				e.Control.DragOver    +=new DragEventHandler(Control_DragOver);
				e.Control.DragDrop    +=new DragEventHandler(Control_DragDrop);								
				e.Control.DragStarting+=new DragEventHandler(Control_DragStarting);
#endif
			}
			base.OnControlAdded (e);
			this.RefreshLayout();
		}


		protected internal override void OnControlRemoved(ControlEventArgs e)
		{
			base.OnControlRemoved (e);
			if( typeof(ITabButton).IsAssignableFrom(e.Control.GetType()) ) 
			{
				ITabButton tabButton = (ITabButton)e.Control;
				e.Control.Click -= new MouseEventHandler(TabButton_OnClick); 
				tabButton.CheckedChanged -= new EventHandler(TabButton_OnCheckedChanged); 

				if(this.m_SelectedButton == tabButton)
					this.m_SelectedButton=null;// unselect
				if(this.m_LastShiftButton == tabButton)
					this.m_LastShiftButton=null;// unselect

				if(this.Visible && this.m_SelectedButton==null)
				{
					SelectFirstButton(this.m_Behavior == SelectionBehavior.SingleSelect);
				}

#if DRAG_AND_DROP_SUPPORT
				e.Control.DragOver    -=new DragEventHandler(Control_DragOver);
				e.Control.DragDrop    -=new DragEventHandler(Control_DragDrop);								
				e.Control.DragStarting-=new DragEventHandler(Control_DragStarting);			
#endif
			}
			this.RefreshLayout();
		}



		private void TabButton_OnClick(object sender, 
			System.Windows.Forms.MouseEventArgs e) 
		{

			if(sender==null || !typeof(ITabButton).IsAssignableFrom(sender.GetType()) ) 
			{
				return;
			}

			ITabButton currentButton = (ITabButton)sender;

			if( m_Behavior == SelectionBehavior.SingleSelect || m_Behavior == SelectionBehavior.ManualSelect ) 
			{
				m_LastShiftButton = null;
				this.SelectedButton = currentButton;
			}
			else if( m_Behavior == SelectionBehavior.MultiSelect ) 
			{
				// Multiselect
   
				System.Windows.Forms.Keys keys = System.Windows.Forms.Control.ModifierKeys;
				bool bCtrlPressed = (keys&Keys.Control) == Keys.Control;
				bool bShiftPressed = (keys&Keys.Shift) == Keys.Shift;
               
				if( bCtrlPressed ) 
				{

					if(currentButton.Checked)
					{
						currentButton.Checked = false;
						//m_LastShiftState = false;
					}
					else
					{
						currentButton.Checked = true;
						//m_LastShiftState = true;
					}

					/*if( m_SelectedButton == currentButton ) 
					{
						if( !currentButton.Checked ) 
						{
							m_SelectedButton = null;

							// m_SelectedButton = first checked
							foreach( Control c in Controls ) 
							{
								if( typeof(ITabButton).IsAssignableFrom(c.GetType()) ) 
								{
									ITabButton tabButton = (ITabButton)c; 
									if( tabButton.Visible && tabButton.Checked ) 
									{
										m_SelectedButton = tabButton;
										break;
									}
								}
							} // foreach
						}
					}
					else
					{
						m_SelectedButton = currentButton;
					}*/
					m_SelectedButton = currentButton;
					m_LastShiftButton = currentButton;
				}
				else if( bShiftPressed ) 
				{
					// TODO: ShiftKeyUp -- m_SelectedButton = currentButton
					//m_LastShiftButton = currentButton;

					//float first = -1, last = -1,  m = -1;

					// has no selection ? m_LastShiftButton = first item from container
					if( m_LastShiftButton == null ) 
					{
						foreach( Control c in Controls ) 
						{
							if( typeof(ITabButton).IsAssignableFrom(c.GetType()) ) 
							{
								ITabButton tabButton = (ITabButton)c; 
								if( tabButton.Visible ) 
								{
									m_LastShiftButton = tabButton;
									//m_LastShiftState = true;
									break;
								}
							}
						}
					}

					/*Control currentButtonCtl = currentButton as Control;
					Control selectedButtonCtl = m_LastShiftButton as Control;

					// get btn range to be checked
					if( m_Direction == LayoutDirection.Vertical ) 
					{
						first = currentButtonCtl.DisplayRectangle.Top+currentButtonCtl.DisplayRectangle.Height/2;
						last = selectedButtonCtl.DisplayRectangle.Top+selectedButtonCtl.DisplayRectangle.Height/2;
					}
					else 
					{
						first = currentButtonCtl.DisplayRectangle.Left+currentButtonCtl.DisplayRectangle.Width/2;
						last = selectedButtonCtl.DisplayRectangle.Left+selectedButtonCtl.DisplayRectangle.Width/2;
					}

					if( first > last ) 
					{
						m = last;
						last = first;
						first = m;
					}*/

					bool _LastShiftState = this.m_SelectedButton==null?true:this.m_SelectedButton.Checked;
					bool setup = false;
					bool clear = false;
					// check btns
					foreach( Control c in Controls ) 
					{
						ITabButton tabButton = c as ITabButton; 
						if( tabButton!=null &&  tabButton.Visible) 
						{
							bool preclear = ( tabButton==m_SelectedButton || tabButton==m_LastShiftButton );
							bool presetup = ( tabButton==currentButton || tabButton==m_LastShiftButton );
							if(!clear && preclear)
							{
								clear = true;
								preclear = false;
							}
							if(!setup && presetup ) 
							{
								setup = true;
								presetup = false;
							}
							if(setup)
							{
								tabButton.Checked = _LastShiftState;
							}
							else if(clear && _LastShiftState)
							{
								tabButton.Checked = false;
							}
							preclear |= ( tabButton==m_SelectedButton && tabButton==m_LastShiftButton );
							presetup |= ( tabButton==currentButton && tabButton==m_LastShiftButton );
							if(clear && preclear)
							{
								clear = false;
							}
							if(setup && presetup ) 
							{
								setup = false;
							}
						}
					} // foreach
					m_SelectedButton = currentButton;
				}
				else 
				{
					//this.m_LastShiftState = true;
					SetCheckedButtons(currentButton);
					m_LastShiftButton = currentButton;
				}

				RefreshLayout();
				OnSelectedTabChanged(System.EventArgs.Empty);
			} // multi-select           
		}

		public void SetCheckedButtons(ITabButton button)
		{
			foreach( Control c in Controls ) 
			{
				if( typeof(ITabButton).IsAssignableFrom(c.GetType()) ) 
				{
					ITabButton tabButton = (ITabButton)c; 
					tabButton.Checked = (button == tabButton);
				}
			}

			m_SelectedButton = button;
		}

		private void TabButton_OnCheckedChanged(object sender, 
			System.EventArgs e)
		{
			ITabButton button = sender as ITabButton;
			if(sender!=null && button!=null)
			{

				if(m_Behavior == SelectionBehavior.SingleSelect && button.Checked)
				{
					this.SelectedButton = button;
				}
				else if(m_Behavior == SelectionBehavior.MultiSelect)
				{
					this.SelectFirstButton(false);
				}
			}
		}


		private void ScrollLeftButton_Click(object sender, 
			System.Windows.Forms.MouseEventArgs e) 
		{
			--m_LeftFirst;
			RefreshLayout();
		}


		private void ScrollRightButton_Click(object sender, 
			System.Windows.Forms.MouseEventArgs e) 
		{
			++m_LeftFirst;
			RefreshLayout();
		}


		/*public void AddTab(int index, ITabButton tab, Object o) 
		{
			Controls.Add(tab);
			RefreshLayout();     
		}*/

		//
		//
		//

		public void RefreshLayout() 
		{
			int i;
			float u;
			bool bs = !(m_ScrollRightButton == null || m_ScrollLeftButton == null);

			SizeF size = this.Size;
			SizeF isize;
			SizeF scrollersSize;

			if( bs ) 
				isize = m_ScrollRightButton.Size;
			else 
				isize = new Size(0, 0);

			if( m_Direction == LayoutDirection.Vertical ) 
			{
				scrollersSize = new SizeF(isize.Width, isize.Height*2+5);
			}
			else 
			{
				scrollersSize = new SizeF(isize.Width*2+5, isize.Height*2);
			}

			// calculate the sum width of all tabs... need scroling????
			float totalWidth = 0;
			int tabCnt = 0;

			ArrayList alWidths = new ArrayList();

			foreach( Control control in Controls ) 
			{
				ITabElement tabElement = (control as ITabElement);
				if( tabElement!=null ) 
				{
					if( tabElement.Visible && tabElement.Fellowship ) 
					{
						if( m_Direction == LayoutDirection.Vertical ) 
						{
							totalWidth += control.DisplayRectangle.Height;
							alWidths.Add(control.Size.Height);
						}
						else 
						{
							totalWidth += control.DisplayRectangle.Width;
							alWidths.Add(control.Size.Width);
						}
						tabCnt++;
					}
				}
			}
			totalWidth += (tabCnt - 1) * m_Interval;

			#region Vertical
			if( m_Direction == LayoutDirection.Vertical ) 
			{
				if( totalWidth >= size.Height ) 
				{
					m_Scrolling = bs & true;
					size.Height -= scrollersSize.Height;

					if( m_LeftFirst < 0 ) 
						m_LeftFirst = 0;

					// i = number of visible tabs from right side
					for( u = 0, i = 0; i < alWidths.Count; i++ ) 
					{
						u += (float)alWidths[alWidths.Count-i-1];
						if(i>0) u+= m_Interval;
						if( u > size.Height ) 
							break;
					}

					if( m_LeftFirst > (tabCnt-i) )
						m_LeftFirst = tabCnt-i;

				}
				else 
				{
					m_LeftFirst = 0;
					m_Scrolling = false;
				}
			}
			else 
			{
				if( totalWidth >= size.Width ) 
				{
					m_Scrolling = true & bs;
					size.Width -= scrollersSize.Width;

					if( m_LeftFirst < 0 ) 
						m_LeftFirst = 0;

					// i = number of visible tabs from right side
					for( u = 0, i = 0; i < alWidths.Count; i++ ) 
					{
						u += (float)alWidths[alWidths.Count-i-1];
						if(i>0) u+= m_Interval;
						if( u > size.Width ) 
							break;
					}

					if( m_LeftFirst > (tabCnt-i) )
						m_LeftFirst = tabCnt-i;

				}
				else 
				{
					m_LeftFirst = 0;
					m_Scrolling = false;
				}
			}
			#endregion

			//
			if(m_Direction == LayoutDirection.Vertical)
			{
				u = ClientRectangle.Top;
			}
			else
			{
				u = ClientRectangle.Left;
			}

			i = 0;
			foreach( Control control in Controls ) 
			{
				ITabElement tabElement = (control as ITabElement);
				if( tabElement!=null ) 
				{
					if( tabElement.Visible && tabElement.Fellowship ) 
					{
						int ci = i++;

						if( m_LeftFirst > ci ) 
						{
							control.Visible = false;
							continue;
						}

						if( m_Direction == LayoutDirection.Vertical ) 
						{
							SizeF curImgSize = control.Size;
							control.SetBounds(0, u, curImgSize.Width, curImgSize.Height, BoundsSpecified.All);
							u += curImgSize.Height;

							control.Visible = (m_LeftFirst == ci) || !(u > size.Height)|!bs;
						}
						else 
						{
							SizeF curImgSize = control.Size;
							control.SetBounds(u, 0, curImgSize.Width, curImgSize.Height, BoundsSpecified.All);
							u += curImgSize.Width;

							control.Visible = (m_LeftFirst == ci) || !(u > size.Width)|!bs;
						}
						u += m_Interval;
					}
					else 
					{
						control.Visible = false;
					}
				}
			}

			#region Scrolling
			//
			if( m_Scrolling ) 
			{            
				if( m_Direction == LayoutDirection.Vertical ) 
				{
					m_ScrollLeftButton.SetBounds(5, 
						ClientRectangle.Bottom-scrollersSize.Height, 
						isize.Width, isize.Height, 
						BoundsSpecified.All);

					m_ScrollRightButton.SetBounds(5, 
						ClientRectangle.Bottom-scrollersSize.Height+isize.Width,
						isize.Width, isize.Height, 
						BoundsSpecified.All);
				}
				else 
				{
					m_ScrollLeftButton.SetBounds(ClientRectangle.Right-scrollersSize.Width, 
						(size.Height/2)-(isize.Height/2), 
						isize.Width, isize.Height, 
						BoundsSpecified.All);

					m_ScrollRightButton.SetBounds(ClientRectangle.Right-scrollersSize.Width+isize.Width, 
						(size.Height/2)-(isize.Height/2), 
						isize.Width, isize.Height, 
						BoundsSpecified.All);
				}

				m_ScrollLeftButton.Visible = true;
				m_ScrollRightButton.Visible = true;
			}
			else 
			{
				if( m_ScrollLeftButton != null )
					m_ScrollLeftButton.Visible = false;
				if( m_ScrollRightButton != null )
					m_ScrollRightButton.Visible = false;
			}
			#endregion
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		/*protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
		{
			//RefreshLayout();
			base.OnPaint(e);
		}*/

		internal protected override void OnResize(System.EventArgs e) 
		{
			base.OnResize(e);
			RefreshLayout();
		}

		private void SelectFirstButton(bool selectVisible)
		{
			foreach( Control c in Controls ) 
			{
				if( typeof(ITabButton).IsAssignableFrom(c.GetType()) ) 
				{
					ITabButton tabButton = (ITabButton)c; 
					if( tabButton.Visible ) 
					{
						if(selectVisible)
						{
							this.SelectedButton = tabButton;
							break;
						}
						else
						{
							if( tabButton.Checked ) 
							{
								this.m_SelectedButton = tabButton;
								break;
							}
						}
					}
				}
			}
			if(this.m_SelectedButton == null && !selectVisible) SelectFirstButton(true);
		}

		protected override void OnHeaderButtonCheckedChanged(object sender, 
			System.EventArgs e)
		{
			base.OnHeaderButtonCheckedChanged(sender, e);
			if(this.m_Behavior == SelectionBehavior.SingleSelect)
			{
				if(this.Visible)
				{
					if(this.m_SelectedButton==null)
					{
						SelectFirstButton(true);
					}
					else
					{
						if(!this.SelectedButton.Checked)
						{
							this.SelectedButton.Checked = true;
							RefreshLayout();
							OnSelectedTabChanged(System.EventArgs.Empty);
						}
					}
				}
				else
				{
					if(this.SelectedButton!=null)
					{
						this.SelectedButton.Checked = false;
						RefreshLayout();
						OnSelectedTabChanged(System.EventArgs.Empty);
					}
				}
			}
			if(this.m_Behavior == SelectionBehavior.MultiSelect)
			{
				if(this.Visible)
				{
					if(this.m_SelectedButton==null || !this.m_SelectedButton.Checked)
					{
						SelectFirstButton(false);
					}
				}
			}
		}


		protected int m_Interval = 0;		
		[System.ComponentModel.DefaultValue(0)]
		public int Interval
		{
			get
			{
				return m_Interval;
			}
			set
			{
				m_Interval = value;
			}
		}
	}        

	/// <summary>
}
